package com.cm.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.goods.entity.Type;
import com.cm.goods.entity.dto.TypeKeyWordDTO;
import com.cm.goods.mapper.TypeMapper;
import com.cm.goods.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @类 名:TypeServiceImpl </br>
 * @描 述:<br>
 * @日 期:2020/9/23 18:17 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public boolean insert(Type type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",type.getName());
        List<Type> brands = typeMapper.selectList(queryWrapper);
        if(brands.size() > 0){
            return false;
        }
        typeMapper.insert(type);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_id",id);
        List<Type> deleteList=typeMapper.selectList(queryWrapper);
        if(typeMapper.deleteById(id) > 0){
            for (Type type:deleteList){
                if (typeMapper.deleteById(type.getId())<0)
                    return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Type type) {
        if (typeMapper.updateById(type) > 0){
            return true;
        }
        return false;
    }

    @Override
    public IPage<Type> getGoodsList(TypeKeyWordDTO typeKeyWordDTO) {
        Page<Type> page = new Page<>();
        typeKeyWordDTO.setCurrentPage((typeKeyWordDTO.getCurrentPage()-1)*typeKeyWordDTO.getPageSize());
        List<Type> typeList=typeMapper.getTypeList(typeKeyWordDTO);
        page.setRecords(typeList);
        page.setCurrent(typeKeyWordDTO.getCurrentPage());
        page.setSize(typeKeyWordDTO.getPageSize());
        int count=typeMapper.getTypeTotal(typeKeyWordDTO);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<Type> getTypeList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Type> typeList=typeMapper.selectList(queryWrapper);
        queryWrapper.eq("parent_id",0L);
        List<Type> resultList=typeMapper.selectList(queryWrapper);
        getChildren(resultList,typeList);
        return resultList;
    }
    public void getChildren(List<Type> typeList,List<Type> totalList){
        for (Type type:typeList){
            boolean hasChildren=false;
            List<Type> childrenType=new ArrayList<>();
            for (Type type1:totalList){
                if (type1.getParentId().equals(type.getId())){
                    hasChildren=true;
                    childrenType.add(type1);
                }
            }
            if (hasChildren==true){
                type.setChildren(childrenType);
                getChildren(childrenType,totalList);
            }
        }
    }
}
